<html>
<header>
    <meta name="viewport" content="width=device-width,initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no"/>
</header>
<body>
    <h3>2019--2021年终一次性奖金最优计算</h3>
    <button id='refresh' onclick='refresh()'>刷新</button>
    <br>
    <label>请输入 年应纳税所得额</label>
    <input type='text' id='input'></input>
    <br>
    <label>最小单位 默认为1元</label>
    <input type='text' id='step'></input>
    <button id='run'>确认</button>
    <p id='output'></p>
    <p>最佳方案</p>
    <script>
    
    function makePair(a, b) {
        return {
            a,
            b
        }
    }

    function deducationMonth(x) {
        const arr = [
            makePair(36000, 0),
            makePair(144000, 2520),
            makePair(300000, 16920),
            makePair(420000, 31920),
            makePair(660000, 52920),
            makePair(960000, 85920),
        ];
        for (let i = 0; i < arr.length; ++ i) {
            if (x <= arr[i].a) {
                return arr[i].b;
            }
        }
        return 181920;
    }

    function deducationYear(x) {
        x = x/12;
        const arr = [
            makePair(3000, 0),
            makePair(12000, 210),
            makePair(25000, 1410),
            makePair(35000, 2660),
            makePair(55000, 4410),
            makePair(80000, 7160),
        ];
        for (let i = 0; i < arr.length; ++ i) {
            if (x <= arr[i].a) {
                return arr[i].b;
            }
        }
        return 15160;
    }

    function preTaxRateMonth(x) {
        const arr = [
            makePair(36000, 3),
            makePair(144000, 10),
            makePair(300000, 20),
            makePair(420000, 25),
            makePair(660000, 30),
            makePair(960000, 35),
        ];
        for (let i = 0; i < arr.length; ++ i) {
            if (x <= arr[i].a) {
                return arr[i].b;
            }
        }
        return 45;
    }

    function preTaxRateYear(x) {
        x = x/12;
        const arr = [
            makePair(3000, 3),
            makePair(12000, 10),
            makePair(25000, 20),
            makePair(35000, 25),
            makePair(55000, 30),
            makePair(80000, 35),
        ];
        for (let i = 0; i < arr.length; ++ i) {
            if (x <= arr[i].a) {
                return arr[i].b;
            }
        }
        return 45;
    }


    function calcTax(m, y) {
        const taxM = m*preTaxRateMonth(m)/100 - deducationMonth(m);
        const taxY = y*preTaxRateYear(y)/100 - deducationYear(y);
        return {
            tax: taxM + taxY,
            taxM,
            taxY,
        };
    }

    const start_btn = document.getElementById('run');
    const enumRes = [];
    function refresh() {
        window.location.href = window.location.href;
    }
    start_btn.addEventListener('click', () => {
        const val = parseInt(document.getElementById('input').value);
        const step = parseFloat(document.getElementById('step').value) || 1;
        /*if (val < 50000) {
            alert('工资太少，不管');
            refresh();
            return;
        } else if (val > 10000000) {
            alert('工资太高，不管');
            refresh();
            return;
        }*/
        let max = -1;
        let lastM = 0;
        let lastY = 0;
        let lastTaxM = 0;
        let lastTaxY = 0;
        console.log('step = ', step);
        for (let i = 0; i <= val; i += step) {
            const m = i;
            const y = val-i;
            const {tax, taxM, taxY} = calcTax(m, y);
            const money = val - tax;
            if (max < money) {
                max = money;
                lastM = m;
                lastY = y;
                lastTaxM = taxM;
                lastTaxY = taxY;
            }
            enumRes.push({
                money,
                m,
                y,
                taxM,
                taxY,
            });
        }
        //const msg = `最大税后收入：${max} 月总收入：${lastM} 年终奖：${lastY} 月扣税：${lastTaxM} 年扣税：${lastTaxY}`;
        const msg = `最优纳税额：${val-max} 最优税后收入：${max}`;
        const o = document.getElementById('output');
        o.innerText = msg;
        const maxEnums = [];
        for (let i = 0; i < enumRes.length; ++ i) {
            const e = enumRes[i];
            if (e.money >= max) {
                maxEnums.push(e);
            }
        }

        function isValidE(e) {
            return e.m >= 0;
        }

        function buildInterval(e0, e1) {
            e0.build = 1;
            e1.build = 1;
            return {
                e0,
                e1,
            }
        }

        let lastIndex = {m:-1};
        let lastStart = {m:-1};
        const intervals = [];
        for (let i = 0; i < maxEnums.length; ++ i) {
            const e = maxEnums[i];
            if (e.m - lastIndex.m > step) {
                if (isValidE(lastStart) && isValidE(lastIndex)) {
                    intervals.push(buildInterval(lastStart, lastIndex));
                }
                lastStart = e;
            }
            lastIndex = e;
        }

        if (!lastStart.build) {
            if (lastStart.m == -1) {
                lastStart = lastIndex;
            }
            intervals.push(buildInterval(lastStart, lastIndex));
        }

        function buildIntervalMsg(interval) {
            if (interval.e0.m == interval.e1.m) {
                return `（单一方案）月工资应纳税所得额：${interval.e0.m} 年终奖应纳税所得额：${interval.e0.y}`;
            } else {
                return `（多选方案）月工资应纳税所得额：${interval.e0.m}~${interval.e1.m} 年终奖应纳税所得额：${interval.e0.y}~${interval.e1.y}`;
            }
        }

        function printInterval(interval) {
            const e = document.createElement('p');
            const msg = buildIntervalMsg(interval);
            e.innerText = msg;
            document.body.append(e);
        }
        for (let i = 0; i < intervals.length; ++ i) {
            printInterval(intervals[i]);
        }
    });
    
    </script>
</body>
</html>